﻿using Max.PH4.Shoppgin.Admin.Domain;
using Max.PH4.Shoppgin.Admin.IRespository;
using Max.PH4.Shoppgin.Admin.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH4.Shoppgin.Admin.Service
{
    public class CategoryService: ICategoryService
    {

        ICategoryRespository _respository;

        public CategoryService(ICategoryRespository respository)
        {
            _respository = respository;
        }

        public List<dynamic> GetRecursionCategory()
        {
            var data = _respository.Query().ToList();
            var list = this.RecursionCategory(data, Guid.Empty);
            return list;
        }


        //递归分类
        private List<dynamic> RecursionCategory(List<CategoryEntity> data,Guid id)
        {
            List<dynamic> categories = new List<dynamic>();

            var list = data.Where(x => x.ParentId == id);
            list.ToList().ForEach(p => {

                //将查询到的值保存起来，拼接成前台树或级联需要的数据格式
                //categories.Add(new
                //{
                //    value = p.Id,
                //    label = p.Name,
                //    children = RecursionCategory(data, p.Id)
                //});

                //上面注释的代码中，子节点也会有空数组出现，在前台没法选择数据，所以拿出来处理
                var d = RecursionCategory(data, p.Id);
                categories.Add(new
                {
                    id = p.Id,
                    name = p.Name,
                    children = d != null && d.Count > 0 ? d : null
                });
            });
            return categories;
        }


        /// <summary>
        /// //斐波拉锲
        /// </summary>
        /// <param name="n">n表示第几位</param>
        /// <returns></returns>        
        private int F(int n)
        {
            if (n == 1 || n == 2) return 1;

            return F(n - 1) + F(n - 2);
        }

        public (bool, string) Insert(CategoryEntity entity)
        {
            throw new NotImplementedException();
        }

        public int Update(CategoryEntity entity)
        {
            throw new NotImplementedException();
        }

        public int Delete(CategoryEntity entity)
        {
            throw new NotImplementedException();
        }

        public int Delete(object id)
        {
            throw new NotImplementedException();
        }

        public List<CategoryEntity> Query()
        {
            throw new NotImplementedException();
        }

        public IQueryable<CategoryEntity> Query(string name)
        {
            throw new NotImplementedException();
        }

        public IQueryable<CategoryEntity> Query(string name, int pageIndex, int pageSize, out int total)
        {
            throw new NotImplementedException();
        }
    }
}
